﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
 
namespace Akiha.Core
{
    /// <summary>
    /// AO的数据和.NET数据的转换桥梁
    /// </summary>
    public static class  DataAdaptorForAO
    {
        //ITable对象转换成DataTable对象
        public static DataTable ITableToDataTable(ITable table)
        {
            DataTable dt = new DataTable("Temp");
            try
            {   //true返回的是同一引用对象，
                //false返回的是独立且不相同的引用对象。
                //这里只是作显示书记，使用true有助于提高性能，一般用于drawing显示操作
                ICursor pCursor = table.Search(null, true);
                
                IRow pRow = pCursor.NextRow();
                if (pRow != null)
                {
                    DataColumn dataColumn = null;
                    //添加字段的信息
                    //只作基础的显示功能，没有考虑具体的字段类型
                    for (int i = 0; i < pRow.Fields.FieldCount; i++)
                    {                                                              
                            dataColumn = dt.Columns.Add(pRow.Fields.get_Field(i).AliasName);
                            //dataColumn.ReadOnly = true;                                                              
                    }
                    while (pRow != null)
                    {
                        DataRow pDataRow = dt.NewRow();
                        for (int j = 0; j < pCursor.Fields.FieldCount; j++)
                        {
                            pDataRow[j] = pRow.get_Value(j);
                        }
                        dt.Rows.Add(pDataRow);
                        pRow = pCursor.NextRow();
                    }
                }
                //释放相应变量
                Marshal.ReleaseComObject(pCursor);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("转换出错，" + ex.Message, "提示");
            }
            return dt;
        }


        //ITable对象转换成字段信息的DT对象
        public static DataTable ITableToFieldsDT(ITable table)
        {
            DataTable dt = new DataTable("tmp");
            try
            {

                string[] strCols = { "Name", "AliasName", "Length", "Type" };
                foreach (string strCol in strCols)
                {
                    dt.Columns.Add(strCol);
                }
                IField pField = null;
                DataRow pDataRow = null;
                string strName = "";
                string strAliasName = "";
                string strLength = "";
                string strType = "";
                for (int i = 0; i < table.Fields.FieldCount; i++)
                {
                    pField = table.Fields.get_Field(i);
                    pDataRow = dt.NewRow();

                    strName = pField.Name;
                    strAliasName=pField.AliasName;
                    strLength=pField.Length.ToString();
                    strType=pField.Type.ToString();

                    pDataRow[0] = strName;
                    pDataRow[1] = strAliasName;
                    pDataRow[2] = strLength;
                    pDataRow[3] = strType;

                    dt.Rows.Add(pDataRow);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            return dt;
        }

        //listFea对象转换成dataTable对象
        //后续考虑扩展其它非要素数据
        public static DataTable ListFeaToDataTable(List<IFeature> lstFea)
        {
            DataTable dt = new DataTable("tmp");
            if (lstFea.Count == 0)
            {
                return dt;
            }
            IRow pRow = lstFea[0];

            DataColumn dataColumn = null;
            //添加字段的信息
            //只作基础的显示功能，没有考虑具体的字段类型
            for (int i = 0; i < pRow.Fields.FieldCount; i++)
            {
                dataColumn = dt.Columns.Add(pRow.Fields.get_Field(i).AliasName);
                //dataColumn.ReadOnly = true;                                                              
            }

            foreach (IFeature pFea in lstFea)
            {
                DataRow pDataRow = dt.NewRow();
                for (int j = 0; j < pRow.Fields.FieldCount; j++)
                {
                    pDataRow[j] = pRow.get_Value(j);
                }
                dt.Rows.Add(pDataRow);
            }

            return dt;

        }
            

           


            
            
        


    }
}
